/**
 * 创建时间： 2016年4月26日 下午3:42:59
 * @author 
 * @version 1.0
 * 文件名称: SystemApi.java
 * 类说明：
 *
 */
package com.itbegin.api;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.itbegin.exceptions.ModelExistsException;
import com.itbegin.model.User;
import com.itbegin.service.account.UserService;
import com.itbegin.utils.StrUtil;

@RestController
@RequestMapping("/api/system")
public class SystemApi {

	@Autowired
	private UserService userService;
	

	@RequestMapping(value="/reg",method=RequestMethod.POST)
	public ApiCommonResult reg(@RequestBody User user){
		List<Msg> msgs = new ArrayList<Msg>();
		try{
			if(user.getName() == null || user.getName().equals("") ){
				msgs.add(new Msg("nameMsg", "请填写姓名."));
			}
			if(user.getEmail() == null || user.getEmail().equals("")  || !StrUtil.isEmail(user.getEmail())){
				msgs.add(new Msg("emailMsg", "请正确填写Email."));
			}
			if(user.getPwd() == null || user.getPwd().equals("") || !user.getPwd().equals(user.getPwd2())){
				msgs.add(new Msg("pwdMsg", "请正确填写密码."));
			}
			if(user.getPhone() == null || user.getPhone().equals("") || !StrUtil.isMobile(user.getPhone())){
				msgs.add(new Msg("phoneMsg", "请正确填写手机号码."));
			}
			if(msgs.isEmpty()){
				userService.reg(user);
				return new ApiCommonResult(true, 1, "新会员注册成功.");
			}else{
				return new ApiCommonResult(false, 1, "新会员注册失败.",msgs);
			}
		}catch(ModelExistsException mex){
			msgs.add(new Msg("nameMsg", mex.getMessage()));
			return new ApiCommonResult(false, 1, "新会员注册失败.",msgs);
		}
		catch(Exception ex){
			return  new ApiCommonResult(false, 1, "新会员注册失败.",ex.getMessage());
		}
	}
}

